package org.jeecg.modules.iot.devices.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.iot.devices.entity.Devices;
import org.jeecg.modules.iot.devices.service.IDevicesService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 设备管理
 * @Author: jeecg-boot
 * @Date:   2021-11-18
 * @Version: V1.0
 */
@Api(tags="设备管理")
@RestController
@RequestMapping("/iot/devices")
@Slf4j
public class DevicesController extends JeecgController<Devices, IDevicesService> {
	@Autowired
	private IDevicesService devicesService;

	/**
	 * 分页列表查询
	 *
	 * @param devices
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "设备管理-分页列表查询")
	@ApiOperation(value="设备管理-分页列表查询", notes="设备管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Devices devices,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Devices> queryWrapper = QueryGenerator.initQueryWrapper(devices, req.getParameterMap());
		Page<Devices> page = new Page<Devices>(pageNo, pageSize);
		Page<Devices> pageList = devicesService.page(page, queryWrapper);
		List<Devices> dl=pageList.getRecords();
		List<Devices> ndl=new ArrayList<>();
		for(Devices dev:dl){
			if(dev.getHeartTime()==null){
				dev.setHeartStatus(0);
			}else{
				long ht= DateUtils.getMillis(dev.getHeartTime()); ;//最近一次心跳时间毫秒数
				long st=DateUtils.getMillis();//系统时间毫秒数
				long hb=dev.getHeartBeat()*1000;//心跳间隔时间
				if(st-hb-ht>0){
					dev.setHeartStatus(0);
				}else{
					dev.setHeartStatus(1);
				}
			}
			ndl.add(dev);
		}
		pageList.setRecords(ndl);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param devices
	 * @return
	 */
	@AutoLog(value = "设备管理-添加")
	@ApiOperation(value="设备管理-添加", notes="设备管理-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Devices devices) {
		devicesService.save(devices);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param devices
	 * @return
	 */
	@AutoLog(value = "设备管理-编辑")
	@ApiOperation(value="设备管理-编辑", notes="设备管理-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Devices devices) {
		devicesService.updateById(devices);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "设备管理-通过id删除")
	@ApiOperation(value="设备管理-通过id删除", notes="设备管理-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		devicesService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "设备管理-批量删除")
	@ApiOperation(value="设备管理-批量删除", notes="设备管理-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.devicesService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "设备管理-通过id查询")
	@ApiOperation(value="设备管理-通过id查询", notes="设备管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Devices devices = devicesService.getById(id);
		if(devices==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(devices);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param devices
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Devices devices) {
        return super.exportXls(request, devices, Devices.class, "设备管理");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Devices.class);
    }

}
